from google.appengine.ext import db

#All students and teachers, the context depends on the course they are in
class People(db.Model):
    uid = db.StringProperty()
    user = db.UserProperty()
    courses = db.ListProperty(db.Key)
    canTeach = db.BooleanProperty()
    #get user by uid
    @staticmethod
    def getPerson(user):
        return People.gql("WHERE uid = :1", user.user_id()).get()
    
#A course, including the person that owns it, so all in it who are not owners are
#students
class Course(db.Model):
    name = db.StringProperty()
    teacher = db.ReferenceProperty(People, collection_name = "TeachCourses")
    description = db.TextProperty()
    #get all the students in the class
    def students(self):
        people = People.gql("WHERE courses = :1", self.key())
        students = []
        for person in people:
            if person.uid != self.teacher.uid:
                students.append(person)
        return students   

#A category in a course that contains a list of questions
class Category(db.Model):
    name = db.StringProperty()
    subtitle = db.StringProperty()
    course = db.ReferenceProperty(Course, collection_name = "Categories")
    questions = db.ListProperty(db.Key)
    isDefault = db.BooleanProperty()    

#A question which potentially links to many directories
class Question(db.Model):
    name = db.StringProperty()
    text = db.TextProperty()
    teacher = db.ReferenceProperty(People, collection_name = "questions")    
    date = db.DateTimeProperty(auto_now_add = True)
    timeToAnswer = db.IntegerProperty()
    endDate = db.DateTimeProperty()
    options = db.StringListProperty()
    answer = db.StringProperty(choices=("A", "B", "C", "D", "E"))
    active = db.BooleanProperty()
    showData = db.BooleanProperty()
    responsesVisible = db.BooleanProperty()      
    #get all directories that contain this question
    def categories(self):
        return Category.gql("WHERE questions = :1", self.key()).fetch()

#One student's answer to a question
class Answer(db.Model):
    studentResponse = db.StringProperty(choices=("A", "B", "C", "D", "E")) 
    student = db.ReferenceProperty(People, collection_name = "Answers")
    question = db.ReferenceProperty(Question, collection_name = "Answers")
    category = db.ReferenceProperty(Category, collection_name = "Answers")
    date = db.DateTimeProperty(auto_now = True)

#A user's clarification
class Clarification(db.Model):
    student = db.ReferenceProperty(People, collection_name = "Clarifications")
    course = db.ReferenceProperty(Course, collection_name = "Clarifications")
    name = db.StringProperty()
    text = db.TextProperty()
    date = db.DateTimeProperty()

#a link between a short string and a course
class CKey(db.Model):
    uKey = db.StringProperty()
    course = db.ReferenceProperty(Course)    
    date = db.DateTimeProperty(auto_now_add = True)